package com.yuncheng.oaApi.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.busService.OaFlowNodeDbrService;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaFlowNodeConstant;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.oaApi.mapper.OaYcSysUserMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.oaApi.service.IOaSysRoleService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.service.IOaSysFlowNodeService;
import com.yuncheng.oaApi.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 流程节点
 * @Author: yuncheng
 * @Date: 2023-10-12
 * @Version: V1.0
 */
@Api(tags = "流程节点接口")
@RestController
@RequestMapping("/api/spzx-oa/sysFlowNode")
public class SysFlowNodeController extends BaseController<OaSysFlowNode, IOaSysFlowNodeService> {

    private static final Logger log = LoggerFactory.getLogger(SysFlowNodeController.class);

    @Resource
    @Lazy
    private IOaSysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;


    @Resource
    @Lazy
    private OaFlowNodeDbrService oaFlowNodeDbrService;


    @Resource
    @Lazy
    private IOaSysRoleService sysRoleService;

    @Resource
    @Lazy
    private OaYcSysUserMapper oaYcSysUserMapper;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;




    /**
     * 分页列表查询
     *

     * @param
     * @return
     */
    @AutoLog(value = "流程节点-分页列表查询")
    @ApiOperation(value = "流程节点-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(String mbId) {
        List<OaSysFlowNode> list = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getFlcmbid, mbId));
        return HttpResult.ok(list);
    }



    /**
     * 添加
     *
     * @param oaSysFlowNode
     * @return
     */
    @AutoLog(value = "流程节点-添加")
    @ApiOperation(value = "流程节点-添加")
    //@RequiresPermissions("flow_node:SysFlowNode:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaSysFlowNode oaSysFlowNode) throws InvocationTargetException, IllegalAccessException {
        Result result = sysFlowNodeService.addNode(oaSysFlowNode);
        //添加日志
        spcyLogService.addLogs(oaSysFlowNode.getFjdmc(), OaFlowNodeConstant.LOG_ADD_NODENAME+":"+ oaSysFlowNode.getFjdmc(),
                                oaSysFlowNode.getId(), OaFlowNodeConstant.LOG_ADD_NODENAME, OaFlowNodeConstant.LOG_ADD_NODENAME);
        return HttpResult.ok(result);
    }



    /**
     * 添加
     *
     * @param mbId 流程模板id
     * @return
     */
    @AutoLog(value = "流程节点-添加第一个节点")
    @ApiOperation(value = "流程节点-添加第一个节点")
    //@RequiresPermissions("flow_node:SysFlowNode:add")
    @GetMapping(value = "/addfirstnode")
    public HttpResult<?> addfirstnode(String mbId){
        Result result = sysFlowNodeService.addFirstNode(mbId);

        List<OaSysFlowNode> oaSysFlowNodes = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getFlcmbid, mbId));
        OaSysFlowNode oaSysFlowNode = oaSysFlowNodes.get(0);
        //添加日志
        spcyLogService.addLogs(oaSysFlowNode.getFjdmc(), OaFlowNodeConstant.LOG_ADD_FIRSTNODENAME+":"+ oaSysFlowNode.getFjdmc(),
                                            oaSysFlowNode.getId(), OaFlowNodeConstant.LOG_ADD_FIRSTNODENAME, OaFlowNodeConstant.LOG_ADD_FIRSTNODENAME);
        return HttpResult.ok(result);
    }




    /**
     * 编辑
     *
     * @param oaSysFlowNode
     * @return
     */
    @AutoLog(value = "流程节点-编辑")
    @ApiOperation(value = "流程节点-编辑")
    //@RequiresPermissions("flow_node:SysFlowNode:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaSysFlowNode oaSysFlowNode) {
        sysFlowNodeService.updateById(oaSysFlowNode);
        //添加日志
        spcyLogService.addLogs(oaSysFlowNode.getFjdmc(), OaFlowNodeConstant.LOG_EDIT_NODENAME+":"+ oaSysFlowNode.getFjdmc(),
                oaSysFlowNode.getId(), OaFlowNodeConstant.LOG_EDIT_NODENAME, OaFlowNodeConstant.LOG_EDIT_NODENAME);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "流程节点-通过id删除")
    @ApiOperation(value = "流程节点-通过id删除")
    //@RequiresPermissions("flow_node:SysFlowNode:delete")
    @GetMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        //先查询拿到对象，防止删除后拿不到
        List<OaSysFlowNode> oaSysFlowNodes = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getId, id));
        OaSysFlowNode oaSysFlowNode = oaSysFlowNodes.get(0);

        sysFlowNodeService.removeId(id);
        //添加日志
        spcyLogService.addLogs(oaSysFlowNode.getFjdmc(), OaFlowNodeConstant.LOG_DELELT_NODENAME+":"+ oaSysFlowNode.getFjdmc(),
                oaSysFlowNode.getId(), OaFlowNodeConstant.LOG_DELELT_NODENAME, OaFlowNodeConstant.LOG_DELELT_NODENAME);

        return HttpResult.ok(I18nUtil.message("delete.success"));
    }



    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "流程节点-通过id查询")
    @ApiOperation(value = "流程节点-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        OaSysFlowNode oaSysFlowNode = sysFlowNodeService.getById(id);
        return HttpResult.ok(oaSysFlowNode);
    }


    @AutoLog(value = "查询当前所有部门")
    @ApiOperation(value = "查询当前所有部门")
    @GetMapping(value = "/getDepart")
    public HttpResult<?> getDepart() {
        List<YcSysOrg> depart = sysFlowNodeService.getDepart();
        return HttpResult.ok(depart);
    }


    @AutoLog(value = "查询当前所有岗位")
    @ApiOperation(value = "查询当前所有岗位")
    @GetMapping(value = "/getJob")
    public HttpResult<?> getJob() {
        List<OaSysJob> job = sysFlowNodeService.getJob();
        return HttpResult.ok(job);
    }

    @AutoLog(value = "查询所有的中心职员")
    @ApiOperation(value = "查询所有的中心职员")
    @GetMapping(value = "/getZxzy")
    public HttpResult<?> getZxzy(String zxzyid) {
        if (StringUtils.isBlank(zxzyid)){
            List<OaSysRole> list = sysRoleService.list(new LambdaQueryWrapper<OaSysRole>().eq(OaSysRole::getRoleName, OaCommonFlowConstant.ROLE_NAME_ZXZY));
            OaSysRole oaSysRoles = list.get(0);
            zxzyid= oaSysRoles.getId();
        }
        List<YcSysUser> zxzy = oaYcSysUserMapper.getZxzy(zxzyid);
        return HttpResult.ok(zxzy);
    }

    /**
     * 数据转换
     * @param oaSysFlowNode
     */
    private void formatValue(OaSysFlowNode oaSysFlowNode){
    }







}
